# 01_jes2012_wave2.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2020-5-16 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("ltm")


# dataset
JES2012 <- read_sav(
  "JESV第2波2012衆院選事後郵送調査.sav", 
  user_na = FALSE
)


# respondent
JES2012_Respondent <- JES2012[,c("id", paste0("q", 8:16, "s1"))]
colnames(JES2012_Respondent)[-1] <- paste0("q", 8:16)


# IRT
## handle missing data
JES2012_Respondent <- JES2012_Respondent[rowSums(JES2012_Respondent[,-1], na.rm = TRUE) != 0,]

## estimate item parameters
irt2012 <- ltm::grm(JES2012_Respondent[,-1], IRT.param = FALSE)
irt2012 ## viewing

## estimate voters ideology
score2012_Respondent <- ltm::factor.scores(irt2012, JES2012_Respondent[,-1])
JES2012_Respondent$Ideology <- -1 * score2012_Respondent$score.dat$z1


# LDP's position
JES2012_LDP <- JES2012[,c("id", "q8s3x1", "q9s3x1", "q10s3x1", "q11s3x1", 
                          "q12s3x1", "q13s3x1", "q14s3x1", "q15s3x1", "q16s3x1")]
colnames(JES2012_LDP)[-1] <- paste0("q", 8:16)
JES2012_LDP <- JES2012_LDP[rowSums(JES2012_LDP[,-1], na.rm = TRUE) != 0,]
score2012_LDP <- ltm::factor.scores(irt2012, JES2012_LDP[,-1])
JES2012_LDP$Ideology <- -1 * score2012_LDP$score.dat$z1


# DPJ's position
JES2012_DPJ <- JES2012[,c("id", "q8s3x2", "q9s3x2", "q10s3x2", "q11s3x2", 
                          "q12s3x2", "q13s3x2", "q14s3x2", "q15s3x2", "q16s3x2")]
colnames(JES2012_DPJ)[-1] <- paste0("q", 8:16)
JES2012_DPJ <- JES2012_DPJ[rowSums(JES2012_DPJ[,-1], na.rm = TRUE) != 0,]
score2012_DPJ <- ltm::factor.scores(irt2012, JES2012_DPJ[,-1])
JES2012_DPJ$Ideology <- -1 * score2012_DPJ$score.dat$z1
